const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

module.exports = {
  formatTime: formatTime
}

function sayHello(name) {
  console.log(`Hello ${name} !`)
}


// 下面开始是自己写的内容

// 冒泡排序
function bubbleSort(originarr, type) {
  var arr = originarr.concat();
  var len = arr.length;
  for (var i = 0; i < len; i++) {
    for (var j = 0; j < len - 1 - i; j++) {
      if (compare(arr[j], arr[j+1], type)) {        //相邻元素两两对比
        var temp = arr[j + 1];        //元素交换
        arr[j + 1] = arr[j];
        arr[j] = temp;
      }
    }
  }
  return arr;
}

function compare(a, b, type) {
  if(type=='health') {
    if (a.health < b.health) {
      return true;
    }
    return false;
  }
  else if(type=='hunger') {
    if (a.hunger < b.hunger) {
      return true;
    }
    return false;
  }
  else if(type == 'sanity') {
    if (a.sanity < b.sanity) {
      return true;
    }
    return false;
  }
  else if (type == 'priority') {
    if (a.priority < b.priority) {
      return true;
    }
    return false;
  }
  
}

// 检测是不是今天第一次获取该数据
function checkFirstGetDataToday(key) {
  var value = wx.getStorageSync(key);
  var date = wx.getStorageSync(key + "date");
}

module.exports.sayHello = sayHello
module.exports.bubbleSort = bubbleSort